/**
 * APISearch
 * Created by yanjd on 2017/9/22.
 */

import {type} from '../type'
let api = {}

/**
 *  获取商品列表
 * @param state
 * @param commit
 * @param params {...}
 * @returns {Promise}
 */
api.getGoodsList = function ({state, commit, rootState}, params) {
    if(params.app){
        params.tradingType = '47,42'
        params.bizOfferTypeId = '2'
        params.couponGrade = '2'
        params.terminalMode = '2'
    }
  return new Promise((resolve, reject) => {
	  //不带有红包参数的列表请求
	  var url = rootState.CONSTANTS.P30006 + 'goodsSearch/goodsSearchList'
	  params.app = false // 关闭app红包功能
	  if(params.app){
		  rootState.axios.post(rootState.CONSTANTS.APISearchCenter + 'OnoffService/selectOnoff', {"mapRequest": {"type": "红包开关"}})
			  .then((res) => {
				  var data = res.body;
				  if(data.success&&data.result.status){
					  //带有红包参数的商品列表请求
					  url=rootState.CONSTANTS.APIGoods + 'goodsSearch/couponGoodsSearchList'
				  }
				  //Promise下必须resolve放在里面 无法提取公用 axios请求无法用同步  只能通过先后发请求或者Promise解决
				  rootState.axios.post(url, params).then(({data}) => {
					  if (!data.success) {
						  alert('服务器爸爸忙不过来了，请尝试重新刷新页面！')
					  }
					  data = JSON.parse(data.result || '{}')
					  resolve(data.items || [])
				  }, () => {
					  //console.error('商品列表请求错误 (╯‵□′)╯︵┻━┻')
					  resolve([])
				  })
			  })
			  .catch(function (res) {
				  console.log(res)
			  });
	  }else{
	  	if (params.new) {
	  		rootState.axios.post(rootState.CONSTANTS.APIMGSearch + 'goodsSearchService/goodsSearch', params).then(({data}) => {
	  			if (!data.success) {
					  alert('服务器爸爸忙不过来了，请尝试重新刷新页面！')
				  }
				  if (!data.respSearch || !data.respSearch.hits) {
				  	resolve([])
				  	return
				  }
				  data.respSearch.hits.forEach(ele => {
						ele.game_id = ele.gameId // 游戏id
						ele.region_name = ele.operatorName // 客户端
						ele.server_name = ele.gameServerName // 服务器
						if (ele.flag && ele.flag.indexOf('~') > -1) {
               ele.is_mjbx = true
            }
						if (ele.flag && ele.flag.indexOf('找') > -1) {
							ele.is_zhbp = true
						}
						ele.goods_img = ''
						if (ele.images) {
							ele.goods_img = JSON.parse(ele.images).ThumbnailsUrl // 首图
						}
						if (ele.imagePathInfos) {
							let list = JSON.parse(ele.imagePathInfos)
							let first = list.shift()
							ele.big_img_list = first.OriginalUrl
							ele.small_img_list = first.ThumbnailsUrl
							list.forEach(img => {
								ele.small_img_list += ';' + img.ThumbnailsUrl
								ele.big_img_list += ';' + img.OriginalUrl
							})
						} else {
							ele.big_img_list = ''
							ele.small_img_list = ''
						}
						delete ele.imagePathInfos
						delete ele.images
				  })
				  resolve(data.respSearch.hits || [])
	  		})
	  	} else {
			 	rootState.axios.post(url, params).then(({data}) => {
				  if (!data.success) {
					  alert('服务器爸爸忙不过来了，请尝试重新刷新页面！')
				  }
				  data = JSON.parse(data.result || '{}')
				  data.items.map(v => {
				  	// 添加卖家保险标识
				  	if (v.biz_offer_flag && v.biz_offer_flag.indexOf('~') > -1) {
            	v.is_mjbx = true
            }
						if (v.biz_offer_flag && v.biz_offer_flag.indexOf('找') > -1) {
							v.is_zhbp = true
						}
				  })
				  resolve(data.items || [])
			  }, () => {
				  //console.error('商品列表请求错误 (╯‵□′)╯︵┻━┻')
				  resolve([])
			  })
	  	}
	  }
  })
}

export default {
  ...api
}
